package com.qianniu.napi.admin.web;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.qianniu.napi.admin.entity.GameAccount;
import com.qianniu.napi.admin.entity.Jinfo;
import com.qianniu.napi.admin.service.IGameAccountService;
import com.qianniu.napi.common.util.ObjectUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.Date;

/**
 *
 * <p>
 * 游戏账户
 * </p>
 *
 * @author chenxuebin
 * @since 2022-02-18
 */
@Controller
public class GameAccountController {

    private static final Logger logger = LoggerFactory.getLogger(GameAccountController.class);

    @Autowired
    private IGameAccountService gameaccountService;

    @RequestMapping("/api/gameaccount/list")
    @ResponseBody
    public Jinfo list(@RequestParam(value="status",required = false)String status,
                      @RequestParam(value = "pageIndex", required = false) Integer pageIndex,
                      @RequestParam(value = "pageSize", required = false) Integer pageSize,
                      HttpServletRequest req) {
        Jinfo info = new Jinfo();
        if(pageSize==null)pageSize = 10;
        Page page=new Page(pageIndex,pageSize);
        page = gameaccountService.selectPage(page, new EntityWrapper<GameAccount>()
                .eq("uid", ObjectUtil.getUid())
                .eq("state", 1));
        info.setInfo(page.getRecords());
        return info;
    }

    @RequestMapping("/api/gameaccount/add")
    @ResponseBody
    public Jinfo gameaccountAdd(@Valid GameAccount gameaccount,
                                BindingResult bindingResult,HttpServletRequest req) {
        Jinfo info = new Jinfo();
        // 字段校验
        if(bindingResult.hasErrors())
        {
            logger.debug( "There are errors! {}", bindingResult );
            info.setMsg("There are errors! "+bindingResult);
            info.setCode(400);
            return info;
        }
        gameaccount.setCreatetime(new Date());
        gameaccount.insert();
        return info;
    }

    @RequestMapping("/api/gameaccount/modify")
    @ResponseBody
    public Jinfo gameaccountUpdate(HttpServletRequest req,
                                   GameAccount gameaccount) {
        Jinfo info = new Jinfo();
        gameaccount.setModifytime(new Date());
        gameaccount.updateById();
        return info;
    }

    @RequestMapping("/api/gameaccount/detail")
    @ResponseBody
    public Jinfo getGameAccountDetailById(@RequestParam("token") String token,
                                          @RequestParam("id") long id,HttpServletRequest req
    ) {
        Jinfo info = new Jinfo();
        GameAccount gameaccount = gameaccountService.selectById(id);
        info.setInfo(gameaccount);
        return info;
    }
}
